package Bench::Test::View::XML;

use 5.008004;
use strict;
use warnings;

use Template;

sub process {
   my $data = shift;
   my $output;

   my $config = {
      INTERPOLATE => 1,  # expand "$var" in plain text
      POST_CHOMP  => 0,  # cleanup whitespace
      PRE_CHOMP   => 0,
      EVAL_PERL   => 1,  # evaluate Perl code blocks
   };

   # create Template object
   my $template = Template->new($config);

   # define template variables for replacement
   my $vars = {
      data => $data,
   };

   # process input template, substituting variables
   $template->process(\*DATA, $vars, \$output)
     || die $template->error();

   return $output;
}


1;

# Template for XML output

__DATA__
[%- PERL -%]
   my $data   = $stash->get('data');
   my $header = shift @$data;
   
   for (@$header) {
      tr/\(\)/\_/;
      s/\_$//;
      $_ = lc;
   }

   $stash->set('header', $header);
[%- END -%]
<?xml version="1.0" encoding="ISO-8859-1" ?>
<data>
[% FOREACH row IN data -%]
   <entry 
   [%- FOREACH item IN row -%]
      [%- index = loop.count - 1 -%]
      [%- field = header.$index  -%]
      [%- field -%]="[%- item -%]" 
   [%- END -%]
/>
[% END -%]
</data>
